import com.jogamp.opengl.GL;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.glu.GLU;

/**
 * Created by WheelChen on 2017/5/18.
 *
 * 矩形
 */
public class Quad implements GLEventListener{
    GLU glu = new GLU();
    @Override
    public void init(GLAutoDrawable glAutoDrawable) {
        System.out.println("正方形初始化");
    }

    @Override
    public void dispose(GLAutoDrawable glAutoDrawable) {
        System.out.println();
    }

    @Override
    public void display(GLAutoDrawable glAutoDrawable) {
        System.out.println("正方形绘制中...");
        final GL2 gl2 = glAutoDrawable.getGL().getGL2();
        gl2.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
//        gl2.glTranslatef(0f, 0f, -2.5f);

        gl2.glBegin(GL2.GL_QUADS);
        gl2.glVertex3f(-0.5f, 0.5f, 0.0f);				// Top Left
        gl2.glVertex3f( 0.5f, 0.5f, 0.0f);				// Top Right
        gl2.glVertex3f( 0.5f,-0.5f, 0.0f);				// Bottom Right
        gl2.glVertex3f(-0.5f,-0.5f, 0.0f);				// Bottom Left
        gl2.glEnd();											// Done Drawing The Quad
    }

    @Override
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {

        System.out.println("窗口大小变动");
        final GL2 gl = drawable.getGL().getGL2();
        //防止被零除
        if (height <= 0) // avoid a divide by zero error!
            height = 1;
        final float h = (float) width / (float) height;
        //设置视窗的大小
        gl.glViewport(0, 0, width, height);
        //选择投影矩阵 ,投影矩阵负责为我们的场景增加透视。
        gl.glMatrixMode(GL2.GL_PROJECTION);
        //重置投影矩阵;
        gl.glLoadIdentity();

        //设置视口的大小
//        glu.gluPerspective(45.0f, h, 1.0, 20.0);
        //启用模型观察矩阵；模型观察矩阵中存放了我们的物体讯息。
        gl.glMatrixMode(GL2.GL_MODELVIEW);

        gl.glLoadIdentity();
    }
}
